class Solution {
    public void moveZeroes(int[] nums) {
        int len = nums.length;

        int left = 0, right = 0;

        //找到第一个 0
        while (left < len && nums[left] != 0) {
            left++;
        }

        right = left + 1;
        //找到 left 后第一个非 0 的数
        while (right < len && nums[right] == 0) {
            right++;
        }

        while (right < len && left < right) {
            swap(nums, left, right);
            while (left < len && nums[left] != 0) {
                left++;
            }
            right = left + 1;
            //找到 left 后第一个非 0 的数
            while (right < len && nums[right] == 0) {
                right++;
            }
        }
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}